package cn.cqie.attendance.student.controller;


import cn.cqie.attendance.common.service.AttachmentService;
import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.student.domian.*;
import cn.cqie.attendance.student.sevice.IStudentLeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfj
 */
@Api(tags = "学生请假模块")
@ApiModel(value = "学生请假模块")
@RestController
@RequestMapping("/studentleave")
public class StudentLeaveController {

    @Autowired
    private IStudentLeaveService istudentLeaveService;
    @Autowired
    private AttachmentService attachmentService;

    /**
     * 根据学生id查询请假记录
     * @return 请假列表
     */
    @GetMapping("/queryLeave")
    @ApiOperation("学生请假列表查询")
    public Page<Leaves> queryLeaves(QueryLeaves queryLeaves){
        return istudentLeaveService.queryLeaves(queryLeaves);
    }

    /**
     * 根据请假id和学生id查询请假详情
     */
    @GetMapping("/queryLeaveDetail")
    @ApiOperation("学生请假详情查询")
    public Page queryLeaveDetail(@RequestParam("id") Long id, @RequestParam("stuId") Long stuId){
        return istudentLeaveService.queryLeaveDetail(id,stuId);
    }

    /**
     * 根据请假id和学生id查询请假日志
     */
    @GetMapping("/queryLeaveLog")
    @ApiOperation("学生请假日志列表查询")
    public Page<LeaveLog> queryLeaveLog(Long id, Long stuId){
        return istudentLeaveService.queryLeaveLog(id,stuId);
    }

    /**
     * 新增请假
     */
    @PostMapping("/addLeave")
    @ApiOperation("学生新增请假")
    public Page addLeave(AddLeaves addLeaves){
        addLeaves.setLeaveStatus(1);
        attachmentService.upload(addLeaves.getFiles(), 3, null, istudentLeaveService.addLeave(addLeaves), null);
        AddLeaveLog addLeaveLog = new AddLeaveLog();
        addLeaveLog.setLeaveId(addLeaves.getId());
        addLeaveLog.setStuId(addLeaves.getStuId());
        addLeaveLog.setLeaveStatus(1);
        addLeaveLog.setTeacherId(addLeaves.getTeacherId());
        addLeaveLog.setTransitTime(addLeaves.getApplyTime());
        istudentLeaveService.addLeaveLog(addLeaveLog);
        return Page.SUCCESS;
    }

    /**
     * 修改请假
     */
    @PostMapping("/updateLeave")
    @ApiOperation("学生修改请假")
    public Page updateLeave(AddLeaves addLeaves){
        istudentLeaveService.updateLeave(addLeaves);
        if (addLeaves.getFiles() != null) {
            attachmentService.upload(addLeaves.getFiles(), 3, null, addLeaves.getId(), null);
        }
        AddLeaveLog addLeaveLog = new AddLeaveLog();
        addLeaveLog.setLeaveId(addLeaves.getId());
        addLeaveLog.setLeaveStatus(1);
        addLeaveLog.setStuId(addLeaves.getStuId());
        addLeaveLog.setTransitTime(addLeaves.getApplyTime());
        addLeaveLog.setTeacherId(addLeaves.getTeacherId());
        istudentLeaveService.addLeaveLog(addLeaveLog);
        return Page.SUCCESS;
    }

    /**
     * 查询学生信息
     */
    @GetMapping("/queryStudent")
    public Page queryStudent(Long id){
        return istudentLeaveService.queryStudent(id);
    }

    /**
     * 查询附件地址
     * @param id 请假id
     * @return
     */
    @GetMapping("/queryAnnex")
    @ApiOperation("学生查询附件地址")
    public Page queryAnnex(Long id){
        return istudentLeaveService.queryAnnex(id);
    }

}
